package com.czd.support.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.czd.base.model.Paginate;
import com.czd.support.common.Configuration;
import com.czd.support.model.Manager;
import com.czd.support.model.ZcncNews;
import com.czd.support.service.ZcncNewsService;

/**
 * @Description 中财内参新闻控制类
 * @author caoren
 * @Date 2015/05/19
 */
@Controller
@RequestMapping(value="/zcncNews")
public class ZcncNewsController extends BaseController{
	@SuppressWarnings("unused")
	private static Log log=LogFactory.getLog(ZcncNewsController.class);
	
	@Autowired
	private ZcncNewsService zcncNewsService;
	
	/**
	 * 中财新闻列表
	 * @param model
	 * @param news
	 * @param pageNo
	 * @return
	 */
	@RequestMapping(value = "/list")
	public String list(Model model,ZcncNews news,@RequestParam(value="pageNo",defaultValue="1") int pageNo){
		news.setPageSize(Configuration.getInstance().getDefault_page_size());
		news.setPageNo(pageNo);
		Paginate paginate = zcncNewsService.queryForPaginate(news);
		model.addAttribute("paginate", paginate);
		return "/zcncnews/list";
	}
	
	/**
	 * 录入新闻页面跳转
	 * @return
	 */
	@RequestMapping(value = "/add",method = RequestMethod.GET)
	public String add(){
		return "/zcncnews/add";
	}
	
	/**
	 * 新闻添加
	 * @param request
	 * @param news
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/add",method = RequestMethod.POST)
	public String add(HttpServletRequest request, ZcncNews news, Model model){
		Manager manager = this.getSesionUser(request);
		news.setCreateUser(manager.getUserCode());
		if(zcncNewsService.insert(news)) {
			return "redirect:/zcncNews/"+news.getId()+"/details";
		}
		return error(model, "操作失败", "录入新闻信息失败");
	}
	/**
	 * 新闻详细
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{id}/details", method = RequestMethod.GET)
	public String details(@PathVariable int id, Model model) {
		ZcncNews news = zcncNewsService.findById(id);
		if(news!=null) {
			model.addAttribute("news", news);
			return "/zcncnews/details";
		}
		return error(model, "新闻信息不存在", "新闻信息不存在");
	}
	
	/**
	 * 跳转到编辑页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
	public String edit(@PathVariable int id, Model model) {
		ZcncNews news = zcncNewsService.findById(id);
		if(news!=null) {
			model.addAttribute("news", news);
			return "/zcncnews/edit";
		}
		return error(model, "新闻信息不存在", "新闻信息不存在");
	}
	
	/**
	 * 更新新闻
	 * @param request
	 * @param news
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.POST) 
	public String edit(HttpServletRequest request, ZcncNews news, Model model){
		Manager manager = this.getSesionUser(request);
		news.setUpdateUser(manager.getUserCode());
		if(zcncNewsService.update(news)) {
			return "redirect:/zcncNews/"+news.getId()+"/details";
		}
		return  error(model, "操作失败", "更新新闻信息失败");
	}
	
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
	public String delete(@PathVariable(value="id") int id, Model model) {
		if(zcncNewsService.delete(id)) {
			return "redirect:/zcncNews/list";
		}
		return error(model, "操作失败", "删除新闻信息失败");
	}
}
